﻿using Learun.Util;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-03-14 10:58
    /// 描 述：业务表
    /// </summary>
    public class ec_business_tableBLL : ec_business_tableIBLL
    {
        private ec_business_tableService ec_business_tableService = new ec_business_tableService();
        #region 获取数据
        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_business_tableEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                var BusinessTableName = queryParam["BusinessTableName"]?.ToString();
                var IsCopyData = queryParam["IsCopyData"]?.ToString();
                var query = Db.Queryable<ec_business_tableEntity>()
                    .WhereIF(!string.IsNullOrEmpty(BusinessTableName), x => x.BusinessTableName.Contains(BusinessTableName))
                    .WhereIF(!string.IsNullOrEmpty(IsCopyData), x => Convert.ToBoolean(x.IsCopyData) == Convert.ToBoolean(IsCopyData))
                    .OrderByDescending(x => x.CreateTime);
                if (pagination == null)
                {
                    return query.ToList();
                }
                else
                {
                    return query.ToPageList(pagination.page, pagination.rows);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_business_tableEntity GetEntity(string keyValue)
        {
            try
            {
                return ec_business_tableService.GetEntity(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue)
        {
            try
            {
                List<int> listProjectIndex = new List<int>();
                var listProject = new ec_projectBLL().GetList("{}").ToList();
                if (listProject != null && listProject.Count > 0)
                {
                    listProjectIndex = listProject.Select(x => Convert.ToInt32(x.ProjectIndex)).Distinct().ToList();
                }
                ec_business_tableService.DeleteEntity(keyValue, listProjectIndex);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ec_business_tableEntity entity)
        {
            try
            {
                List<int> listProjectIndex = new List<int>();
                var listProject = new ec_projectBLL().GetList("{}").ToList();
                if (listProject != null && listProject.Count > 0)
                {
                    listProjectIndex = listProject.Select(x => Convert.ToInt32(x.ProjectIndex)).Distinct().ToList();
                }
                ec_business_tableService.SaveEntity(keyValue, entity, listProjectIndex);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

    }
}
